<template>
  <div>
    <div v-for="(item,index) in newSongList" :key="item.id" class="newDetail" :style="{backgroundColor:index%2?'#252525':'#292929'}">
      <div class="newContent">
        <span>{{currentIndex(index+1)}}</span>

        <div style="position:relative; font-size:0px">
          <img :src="item.picUrl" alt width="60" height="60" loading="lazy" />
          <div class="songPlay">
            <i class="iconfont iconbofang"></i>
          </div>
        </div>
        <div>{{item.name}}</div>
        <div>{{item.singer}}</div>
        <div>{{item.album}}</div>
        <div>{{formatTime(item.duration)}}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, defineProps, onMounted, ref, watch } from 'vue'
import axios from '@/http'
const props = defineProps({
  type: {
    type: Number,
    default: 0
  }
})
const currentIndex = computed(() => index => index < 10 ? '0' + index : index)
watch(
  () => props.type,
  val => {
    getNewSongList()
  }
)
onMounted(() => {
  getNewSongList()
})
const newSongList = ref([])
const formatTime = computed(() => {
  return time => {
    const S = Math.floor(time / 1000)
    const m = Math.floor(S / 60)
    const s = S % 60
    return `${m}:${s < 10 ? '0' + s : s}`
  }
})
const getNewSongList = () => {
  axios
    .get('/top/song', {
      params: {
        type: props.type
      }
    })
    .then(res => {
      console.log(res)
      newSongList.value = res.data.data.map(item => {
        return { ...item, picUrl: item.album.picUrl, singer: item.artists[0].name, album: item.album.name }
      })
    })
}
</script>

<style lang="scss" scoped>
.newDetail {
  padding: 8px 20px;
  position: relative;
  .songPlay {
    width: 26px;
    height: 26px;
    background-color: rgba(255, 255, 255, 0.8);
    position: absolute;
    border-radius: 50%;
    overflow: hidden;
    top: 0px;
    bottom: 0;
    margin: auto;
    left: 0px;
    right: 0px;
    transition: opacity 0.15s;
    opacity: 1;
    &:hover {
      opacity: 1;
      cursor: pointer;
    }
    &::after {
      position: absolute;
      content: '';
      filter: blur(30px);
      background-attachment: scroll;
      width: 30px;
      background-size: 150px 150px;
      background-repeat: no-repeat;
      background-position: -110px -104px;
      left: 0;
      background-color: #b3b3b3;
      height: 30px;
    }
    .iconfont {
      font-size: 13px;
      position: absolute;
      color: red;
      left: 8px;
      top: 6px;
      right: 0;
      bottom: 0;
      margin: auto;
    }
  }
  &:hover {
    background-color: #2d2d2d !important;
  }
  .newContent {
    text-align: center;
    color: #575757;
    display: flex;
    align-items: center;
    div {
      font-size: 12px;
      color: #b2b2b2;
      font-weight: bold;
      margin-left: 16px;
      text-align: left;
      &:nth-child(3) {
        flex-basis: 200px;
      }
      &:nth-child(4) {
        flex-basis: 150px;
      }
      &:nth-child(5) {
        flex-basis: 260px;
      }
    }
    img {
      border-radius: 5px;
      &:hover {
        cursor: pointer;
      }
    }
    span {
      flex-basis: 60px;
      line-height: 60px;
      font-size: 13px;
      margin: 0 10px;
    }
  }
}
</style>